const fs = require('fs')
const Excel = require('exceljs');

/**
 * Excel转Json数据
 * @param {*} src           读取的excel文件目录
 * @param {*} dst           导出的json文件目录
 * @param {*} isClient      是否为客户端数据
 */
const excel2json = async function (src, dst, isClient) {
    const result = {};
    let keys = [];
    let types = [];
    let servers = [];
    let clients = [];

    const workbook = new Excel.Workbook();
    // 读取excel
    await workbook.xlsx.readFile(src);
    const worksheet = workbook.getWorksheet(1); // 获取第一个worksheet 
    worksheet.eachRow((row, rowNumber) => {
        let data = {};
        // cell.type单元格类型：6-公式 ;2-数值；3-字符串
        row.eachCell((cell, colNumber) => {
            const value = cell.value;
            if (rowNumber === 2) {                      // 字段名
                keys.push(value);
            }
            else if (rowNumber === 3) {                 // 字段类型
                types.push(value);
            }
            else if (isClient == false && rowNumber === 4) {
                servers.push(value);
            }
            else if (isClient == true && rowNumber === 5) {
                clients.push(value);
            }
            else {
                let index = colNumber - 1;
                let type = types[index];
                let server = servers[index];
                let client = clients[index];
                let isWrite = isClient && client === "client" || isClient == false && server === "server";
                if (isWrite) {
                    switch (type) {
                        case "int":
                            data[keys[index]] = parseInt(value);
                            break;
                        case "float":
                            data[keys[index]] = parseFloat(value);
                            break;
                        case "string":
                            data[keys[index]] = value;
                            break;
                    }
                }
            }
        });
        if (rowNumber > 5) {
            let id = data["id"];
            delete data["id"];
            result[id] = data;
        }
    });

    // 写入流
    if (result["undefined"] == null) {
        console.log(isClient ? "客户端数据" : "服务器数据", dst);
        console.log(result);
        await fs.writeFileSync(dst, JSON.stringify(result));
    }
    else {
        console.log(isClient ? "客户端数据" : "服务器数据");
        console.log("无");
    }
}

// var inputExcelPath = "D:/Work/Demo/Cocos3D_DEMO/assets/excel/"
// var outJsonPath = "D:/Work/Demo/Cocos3D_DEMO/assets/excel/"
var inputExcelPath = "./excel/"
var outJsonPath = "./"
const files = fs.readdirSync(inputExcelPath);
files.forEach(f => {
    let name = f.substring(0, f.indexOf("."));
    let ext = f.toString().substring(f.lastIndexOf(".") + 1);
    if (ext == "xlsx") {
        // excel2json(inputExcelPath + f, outJsonPath + "server/" + name + ".json", false);        // 服务器数据
        excel2json(inputExcelPath + f, outJsonPath + "client/" + name + ".json", true);         // 客户端数据
    }
});